package com.dinian.zeus.controller;

import com.dinian.zeus.domain.Periodical;
import com.dinian.zeus.service.PeriodicalService;
import com.dinian.zeus.util.Result;
import com.dinian.zeus.util.commonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author dinian
 * @Date 2019-01-02
 */
@Slf4j
@RestController
public class PeriodicalController {

    @Autowired
    private PeriodicalService periodicalService;

    /**
     * 初始化所有报刊信息
     * @return
     */
    @RequestMapping("/find/allPeriodical")
    public List<Periodical> initPeriodical(){
        log.info("init periodical is start...");
        List<Periodical> initPeriodical = periodicalService.findAllPeriodical();
        return initPeriodical;
    }

    /**
     * 添加
     * @param periodical
     * @return
     */
    @RequestMapping("/addPeriodical")
    public Result addPeriodical(Periodical periodical){
        int i = periodicalService.addPeriodical(periodical);
        if (i != 0){
            return Result.ok("添加成功");
        }
        return Result.error("添加失败");
    }

    /**
     * 查询 byID
     * @param id
     * @return
     */
    @RequestMapping("/queryPeriodicalById")
    public Result queryPeriodical(String id){
        log.info("query id: {}",id);
        Periodical periodical = periodicalService.queryPeriodicalById(id);
        return Result.ok(periodical);
    }

    /**
     * 修改
     * @param periodical
     * @return
     */
    @RequestMapping("/modifyPeriodical")
    public Result modifyPeriodical(Periodical periodical){
        log.info(periodical.toString());
        periodicalService.modifyPeriodical(periodical);
        return Result.ok("修改成功");
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @RequestMapping("/deletePeriodical")
    public Result removePeriodical(String ids){
        log.info("delete id: {}",ids);
        String[] strings = ids.split(",");
        periodicalService.removePeriodicals(commonUtil.arrString2Int(strings));
        return Result.ok("删除成功");
    }
}
